package org.example.mallcommon.confing;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

/**
 * @author 韩先楚
 */
@Configuration
public class JsonConfiguration {

    @Bean
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        return builder
                .modules(new JavaTimeModule()) // 支持Java8时间类型
                .featuresToDisable(
                        SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, // 禁用时间戳格式
                        DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES // 忽略未知字段
                )
                .featuresToEnable(
                        DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT // 空字符串转null
                )
                .build();
    }

}
